home *** CD-ROM | disk | FTP | other *** search
/ MacTech 1 to 12 / MacTech-vol-1-12.toast / Source / MacTech® Magazine / Volume 10 - 1994 / 10.09 Sep 94 / Fez (MacHack Winner) / FezEdit ƒ / Utilities.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-07-06  |  2.2 KB  |  100 lines  |  [TEXT/MMCC]

  1. /*
  2.  *    Graphics utility routines
  3.  */
  4.  
  5. #include "Utilities.h"
  6.  
  7. // The PushPort and PopPort routines both operate on a stack of GrafPtrs.
  8. // This rarely becomes more nested than a few levels, but it makes it
  9. // easier to temporarily swap a port into and out of scope without
  10. // having to declare a local stack variable.  In a more general design,
  11. // the stack would hold entire GWorlds.
  12.  
  13. static GrafPtr portStack[MAXPORTSTACK];        // Stack of saved GrafPtrs
  14. static short sp = 0;
  15.  
  16. /*
  17.  *    Save the current port, and set the given port (if non-NIL)
  18.  */
  19.  
  20. void PushPort(GrafPtr port)
  21.     {
  22.         if (sp < MAXPORTSTACK) {
  23.             GetPort(&portStack[sp++]);
  24.             if (port) SetPort(port);
  25.             }
  26.     }
  27.  
  28. /*
  29.  *    Restore the last saved port.
  30.  */
  31.  
  32. void PopPort()
  33.     {
  34.         if (sp > 0)
  35.             SetPort(portStack[--sp]);
  36.     }
  37.  
  38. /*
  39.  *    Convert a rectangle in place from local coordinates to global ones
  40.  *    with respect to the given port.  If port is NIL, this will be the
  41.  *    current port.  This code is Mac specific in that it knows that a
  42.  *    Rect structure is two Point structs next to each other.
  43.  */
  44.  
  45. void LocalToGlobalRect(GrafPtr port, Rect *r)
  46.     {
  47.         PushPort(port);
  48.         LocalToGlobal( (Point *)r );            // Top left
  49.         LocalToGlobal( ((Point *)r) + 1 );        // Bottom right
  50.         PopPort();
  51.     }
  52.  
  53. /*
  54.  *    Convert a rectangle in place from global to local coordinates with
  55.  *    respect to the given port.  If port is NIL, this will be the
  56.  *    current port.  This code is Mac specific like above.
  57.  */
  58.  
  59. void GlobalToLocalRect(GrafPtr port, Rect *r)
  60.     {
  61.         PushPort(port);
  62.         GlobalToLocal( (Point *)r );            // Top left
  63.         GlobalToLocal( ((Point *)r) + 1 );        // Bottom right
  64.         PopPort();
  65.     }
  66.  
  67. /*
  68.  *    Deliver a rectangle, ans, that is the centered version of a
  69.  *    given rectangle, r, within another given rectangle, inside.
  70.  */
  71.  
  72. void CenterRect(Rect *r, Rect *inside, Rect *ans)
  73.     {
  74.         short rx,ry,ix,iy;
  75.  
  76.         // Use the difference between the rectangles' centers as an offset
  77.         
  78.         rx = (r->left + r->right) / 2;
  79.         ry = (r->top + r->bottom) / 2;
  80.         
  81.         ix = (inside->right + inside->left) / 2;
  82.         iy = (inside->bottom + inside->top) / 2;
  83.         
  84.         *ans = *r;
  85.         OffsetRect(ans,ix-rx,iy-ry);
  86.     }
  87.  
  88. /*
  89.  *    Delay by (aBit - .5) ticks, on average
  90.  */
  91.  
  92. void Wait(short aBit)
  93.     {
  94.         long soon = TickCount() + aBit;
  95.         
  96.         while (TickCount() < soon) ;
  97.     }
  98.  
  99.  
  100.